<template>
  <VButton :auth="auth" :loading="loading" @click="update">更新申报信息</VButton>
</template>

<script setup>
import api from './api'
import {resMessage} from '@/utils/res'
import { ElMessage } from 'element-plus'

const props = defineProps({
  auth: { type: String, default: '' },
  mainKey: { type: String, default: '' },
  tableRef: { type: Object, default: () => ({}) },
})

const emit = defineEmits(['success'])

const loading = ref(false)
const update = () => {
  const selection = props.tableRef.$table.getCheckboxRecords()
  if (!selection.length) {
    ElMessage.warning('请先选择行')
    return
  }
  const {mainKey} = props
  loading.value = true
  api.update({[mainKey+'s']: selection.map(d => d[mainKey])}).then(res => {
    resMessage({
      res,
      success: () => {
        ElMessage.success('操作成功')
        emit('success')
      }
    })
  }).finally(() => {
    loading.value = false
  })
}
</script>